package com.flashflexpro.tanksinbattle.battlescene {
import com.flashflexpro.tanksinbattle.battlescene.dto.BattleSceneUserInputVO;

import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

public class UserInputHandler {
    public function UserInputHandler( battleScene:BattleScene) {
        _battleScene = battleScene;
        var stage:Stage = _battleScene.stage;

        stage.addEventListener( KeyboardEvent.KEY_DOWN, onKey );
        stage.addEventListener( KeyboardEvent.KEY_UP, onKey );

        stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouse );
        stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouse );
        stage.addEventListener( MouseEvent.MOUSE_UP, onMouse );
        stage.addEventListener( MouseEvent.MOUSE_WHEEL, onMouse );
    }

    private var _battleScene:BattleScene;


    private function onMouse(event:MouseEvent):void {
        var v:BattleSceneUserInputVO = new BattleSceneUserInputVO();
        v.type = event.type;
        v.ctrlKey = event.ctrlKey;
        v.altKey = event.altKey;
        v.shiftKey = event.shiftKey;
        v.buttonDown = event.buttonDown;
        v.delta = event.delta;
        v.stageX = event.stageX;
        v.stageY = event.stageY;
        v.time = _battleScene.time;
        _battleScene.commands.unshift( v );
        if( _battleScene.commands.length > 24 ){
            _battleScene.commands.pop();
        }
    }

    private var _lastKeyDown:int = -1;
    private function onKey(event:KeyboardEvent):void {
        // a s d f up down left right
        if( [ 37, 38, 39, 40, 65, 68, 87, 83 ].indexOf( event.keyCode ) < 0 ){
            return;
        }

        if( event.type == KeyboardEvent.KEY_DOWN ){
            if( _lastKeyDown < 0 ){
                _lastKeyDown = event.keyCode;
            }
            else if( event.keyCode == _lastKeyDown ){
                return;
            }
        }
        else if( event.type == KeyboardEvent.KEY_UP  && event.keyCode == _lastKeyDown ){
            _lastKeyDown = -1;
        }


        var v:BattleSceneUserInputVO = new BattleSceneUserInputVO();
        v.type = event.type;
        v.ctrlKey = event.ctrlKey;
        v.altKey = event.altKey;
        v.shiftKey = event.shiftKey;
        v.time = _battleScene.time;
        v.keyCode = event.keyCode;

        trace( this + "onKey->" + v );

        _battleScene.commands.unshift( v );
        if( _battleScene.commands.length > 24 ){
            _battleScene.commands.pop();
        }
    }

    public function destroy():void {
        var stage:Stage = _battleScene.stage;

        stage.removeEventListener( KeyboardEvent.KEY_DOWN, onKey );
        stage.removeEventListener( KeyboardEvent.KEY_UP, onKey );
        stage.removeEventListener( MouseEvent.MOUSE_DOWN, onMouse );
        stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMouse );
        stage.removeEventListener( MouseEvent.MOUSE_UP, onMouse );
        stage.removeEventListener( MouseEvent.MOUSE_WHEEL, onMouse );
    }


    public function toString():String {
        return "[UserInputHandler]";
    }
}
}